10  R: Functions

10.1 Functions in R

Functions in R are used to encapsulate reusable code. R provides a wide range of built-in functions, and users can also define their own functions.


10.1.1 Built-in Functions in R

R has many predefined functions for mathematical operations, statistics, and data manipulation.

10.1.1.1 🔹 Common Built-in Functions

sqrt(25)
[1] 5
abs(-10)
[1] 10
round(3.14159, 2)
[1] 3.14
# Character functions
toupper("hello")  # Convert to uppercase → "HELLO"
[1] "HELLO"
tolower("WORLD")  # Convert to lowercase → "world"
[1] "world"
nchar("Hello")    # Count characters → 5
[1] 5
# Logical functions
any(c(TRUE, FALSE, FALSE))   # TRUE if at least one TRUE
[1] TRUE
all(c(TRUE, TRUE, FALSE))    # FALSE if any FALSE
[1] FALSE

10.2 Defining a function

Functions in R are defined using the keyword function(). All the statements within a function are enclosed with {} braces. Look at the function defined below. It takes an integer as an argument, and prints whether the integer is odd or even.

odd_even <- function(intgr) {
  if (intgr %% 2 == 0) {
    print("even")
  } else {
      print("odd")
  }
}

odd_even(3)
[1] "odd"

10.2.1 Function arguments

In both R and Python, functions support multiple types of arguments, including positional arguments, default arguments, variable-length arguments, and keyword arguments. The behavior of function arguments in R is nearly identical to Python.

10.2.2 Practice exercise

Write a function that returns all prime numbers between \(a\) and \(b\), where \(a\) and \(b\) are parameters of the function.

prime <- function(a, b) {
  prime_numbers <- c()
  for (number in a:b) {
    prime = 1
    
    for (factor in 2:(number - 1)) {
      if (number %% factor == 0) {
        prime = 0
      }
    }
    
    if (prime == 1) prime_numbers <- c(prime_numbers, number)
  }
  return(prime_numbers)
}
prime(40, 60)
[1] 41 43 47 53 59